<meta charset="utf-8">


<!-- <script>
    // 之前说过： 迭代器  可迭代对象
    // 可迭代对象的应用场景: 把一个非可迭代对象 转化成 可迭代对象
    const info = {
        name: "码路",
        age: 18,
        height: 1.88,
        [Symbol.iterator]() {
            let values = Object.values(this)
            // console.log(values);
            let index = 0;
            let iterator = {
                next() {
                    if (index < values.length) {
                        return { done: false, value: values[index++] }
                    } else {
                        return { done: true, value: undefined }
                    }
                }
            }
            return iterator;
        }
    };

    let iterator = info[Symbol.iterator]();
    console.log(iterator.next());
    console.log(iterator.next());
    console.log(iterator.next());
    console.log(iterator.next());

</script> -->

















<!-- <script>
    // 可迭代对象的应用场景: 一些类的构造方法中, 也是传入的可迭代对象
    // 语法：new Set([iterable])
    const set = new Set(["aaa", "bbb", "ccc"])
    console.log(set);
    const set2 = new Set("hello")
    console.log(set2);
    const set3 = new Set(set2)
    console.log(set3);
</script> -->














<!-- <script>
    // 可迭代对象的应用场景: 一些常用的方法
    // const p1 = Promise.resolve("aaaa")
    // const p2 = Promise.resolve("bbbb")
    // const p3 = Promise.resolve("cccc")

    // let set = new Set();
    // set.add(p1)
    // set.add(p2)
    // set.add(p3)

    // // all([iterable])
    // Promise.all(set).then(res => {
    //     console.log("res:", res);
    // })


    function bar() {
        // arguement不仅是一个伪数组，也是一个可迭代对象
        console.log(arguments);

        for (let item of arguments) {
            console.log(item);
        }
    }

    bar(111, 222, 333);
</script> -->














<!-- <script>
    // 可迭代对象的应用场景: 自定义类的迭代
    class Person {
        constructor(name, age, height, friends) {
            this.name = name
            this.age = age
            this.height = height
            this.friends = friends
        }

        // 实例方法
        running() { }
        [Symbol.iterator]() {
            let index = 0;
            let iterator = {
                next: () => {
                    if (index < this.friends.length) {
                        return { done: false, value: this.friends[index++] }
                    } else {
                        return { done: true, value: undefined }
                    }
                }
            }
            return iterator;
        }
    }

    // p1和p2不是可迭代对象
    const p1 = new Person("码路", 18, 1.88, ["aaa", "bbb", "ccc"])
    const p2 = new Person("漫漫", 30, 1.98, ["111", "222", "333"])

    for (const item of p2) {
        console.log(item)
    }
</script> -->

<script>
    // 可迭代对象的应用场景: 自定义类的迭代
    class Person {
        constructor(name, age, height, friends) {
            this.name = name
            this.age = age
            this.height = height
            this.friends = friends
        }

        // 实例方法
        running() { }
        *[Symbol.iterator]() {
            
            yield* this.friends;

            // let index = 0;
            // let iterator = {
            //     next: () => {
            //         if (index < this.friends.length) {
            //             return { done: false, value: this.friends[index++] }
            //         } else {
            //             return { done: true, value: undefined }
            //         }
            //     }
            // }
            // return iterator;
        }
    }

    // p1和p2不是可迭代对象
    const p1 = new Person("码路", 18, 1.88, ["aaa", "bbb", "ccc"])
    const p2 = new Person("漫漫", 30, 1.98, ["111", "222", "333"])

    for (const item of p2) {
        console.log(item)
    }
</script>